package com.cn.onetrip.activity;

import java.util.List;

import com.cn.onetrip.adapter.TravelItemAdapter;
import com.cn.onetrip.objects.NotesListObj;
import com.cn.onetrip.activity.R;
import com.cn.onetrip.untility.ActivityTag;
import com.cn.onetrip.untility.SysApplication;
import com.cn.onetrip.untility.TitleScrollManager;
import com.cn.onetrip.untility.Untility;
import com.cn.onetrip.view.AdView;
import com.cn.onetrip.view.ContentScrollView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class TravelActivity extends Activity implements OnItemClickListener {
	// public static View view;
	private ListView travelList;
	private TravelItemAdapter adapter;
	private Button quit;

	private List<NotesListObj> notesList;

	private AdView adView;
	private RelativeLayout layoutTitle;
	private TitleScrollManager titleScrollManager;
	private ContentScrollView scrollView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Window w = getWindow();
		// view = w.getDecorView();
		if (SysApplication.notesList.size() == 0) {
			finish();
			android.os.Process.killProcess(android.os.Process.myPid());
		}
		setContentView(R.layout.activity_travel);
		travelList = (ListView) findViewById(R.id.TravelList);
		adapter = new TravelItemAdapter(TravelActivity.this, travelList,
				handler);
		loadDate();
		travelList.setAdapter(adapter);
		new Untility().setListViewHeightBasedOnChildren(travelList);

		travelList.setOnItemClickListener(this);
		quit = (Button) findViewById(R.id.quit);
		quit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// view.startAnimation(Untility.rQuest1Animation);
				// GroupActivity.changeFirstView();
				finish();
				overridePendingTransition(R.anim.push_left_translate,
						R.anim.pull_right_translate);
			}
		});

		adView = (AdView) findViewById(R.id.ad_view);
		layoutTitle = (RelativeLayout) findViewById(R.id.LayoutTopTravel);
		titleScrollManager = new TitleScrollManager(layoutTitle, adView,
				scrollView, null);
		scrollView = (ContentScrollView) findViewById(R.id.scrollContent);
		// new TitleScrollManager(layoutTitle, adView, scrollView, null);
	}

	@Override
	protected void onResume() {
		adView.startTimer();
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
		adView.stopTimer();
	}

	@Override
	protected void onStop() {
		super.onStop();
		titleScrollManager.stopTimer();
	}

	@Override
	protected void onDestroy() {
		System.gc();
		adapter.clean();
		adapter.threadQuit();
		adapter.clear();
		titleScrollManager.stopTimer();
		titleScrollManager = null;
		super.onDestroy();
	}

	public void loadDate() {
		adapter.clean();
		notesList = SysApplication.notesList;
		for (int i = 0; i < notesList.size(); i++) {
			NotesListObj notesObj = notesList.get(i);
			adapter.addNotes(notesObj.icon, notesObj.title, notesObj.subTitle,
					notesObj.author, notesObj.time);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent intent = new Intent(getApplicationContext(),
				TravelDetailActivity.class);
		Bundle bundle = new Bundle();
		bundle.putInt("title_image", R.drawable.travel_detail_title);
		bundle.putBoolean("Left", true);
		bundle.putString("module", ActivityTag.Travel);
		bundle.putInt("itemId", arg2);
		bundle.putString("title", notesList.get(arg2).title);
		bundle.putString("time", notesList.get(arg2).time);
		bundle.putString("source", notesList.get(arg2).source);
		intent.putExtras(bundle);
		startActivity(intent);
		overridePendingTransition(R.anim.push_right_translate,
				R.anim.pull_left_translate);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			finish();
			overridePendingTransition(R.anim.push_left_translate,
					R.anim.pull_right_translate);
		}
		return super.onKeyDown(keyCode, event);
	}

	public Handler handler = new Handler() {
		public void dispatchMessage(android.os.Message msg) {
			if (msg.what == 0) {
			}
		};
	};
}
